package com.caiyouxi.goods.service;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.goods.mapper.GoodsCategoryMapper;
import com.caiyouxi.goods.model.GoodsCategory;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



/**
 * 商品品类
 * chensong
 */
@Service
public class GoodsCategoryService extends BaseService {

    @Autowired
    private GoodsCategoryMapper goodsCategoryMapper;


    private Logger logger = Logger.getLogger(GoodsCategoryService.class);

    /**
     * 查询商品品类
     */
    public ResData get_goods_category_info(GoodsCategory goodsCategory){
        GoodsCategory newGoodsCategory = select_goods_category(goodsCategory.getId());
        return  success(newGoodsCategory);
    }
    
    public GoodsCategory select_goods_category(Integer id){
        String redis_key = "GOODS_CATEGORY_"+ id;
        logger.info("search redis...");
        GoodsCategory newGoodsCategory = (GoodsCategory) redisService.get(redis_key);
        if (null == newGoodsCategory){
            logger.info("search db...");
            newGoodsCategory  =goodsCategoryMapper.select_goods_category(id);
            redisService.set(redis_key,newGoodsCategory);
        }
        return newGoodsCategory;
    }

    /**
     * 查询商品品类列表
     */
     public ResData get_goods_category_list(GoodsCategory goodsCategory){
        Integer reids_page_size = Integer.parseInt( redisService.get("sys.pagesize").toString());
        if(null != reids_page_size){
            goodsCategory.setPage_size(reids_page_size);
        }
         List <GoodsCategory>  goodsCategorys = goodsCategoryMapper.select_goods_category_list(goodsCategory);
         QueryParams pageList = new QueryParams();
         pageList.setPage(goodsCategory.getPage());
         pageList.setPage_size(goodsCategory.getPage_size());
         pageList.setList(goodsCategorys);
         return success(pageList);
    }

    /**
     * 新增商品品类
     */
    @Transactional
    public ResData add_goods_category(GoodsCategory goodsCategory){
         int result =goodsCategoryMapper.add_goods_category(goodsCategory);
         if(result == 1){
            return success(null);
         }
         return failed(ErrorCode.SYS_GOODSCATEGORY_ADD_ERROR.getCode(),ErrorCode.SYS_GOODSCATEGORY_ADD_ERROR.getMsg(),null);
    }

    /**
     * 修改商品品类
     */
    @Transactional
    public ResData update_goods_category(GoodsCategory goodsCategory){
         int result = goodsCategoryMapper.update_goods_category(goodsCategory);
         if(result == 1){
             String redis_key = "GOODS_CATEGORY_"+goodsCategory.getId();
             redisService.set(redis_key,goodsCategory);  //更新缓存内容
             return success(null);
         }
        return failed(ErrorCode.SYS_GOODSCATEGORY_UPDATE_ERROR.getCode(),ErrorCode.SYS_GOODSCATEGORY_UPDATE_ERROR.getMsg(),null);
    }

    /**
     * 删除商品品类
     */
    @Transactional
    public ResData delete_goods_category(GoodsCategory goodsCategory){
        int result =goodsCategoryMapper.delete_goods_category(goodsCategory.getId());
        if(result == 1){
            String redis_key = "GOODS_CATEGORY_"+goodsCategory.getId();
            redisService.remove(redis_key);  //删除缓存
            return success(null);
        }
        return failed(ErrorCode.SYS_GOODSCATEGORY_DELTET_ERROR.getCode(),ErrorCode.SYS_GOODSCATEGORY_DELTET_ERROR.getMsg(),null);
    }
}
